package com.dualsportmaps.android.activities;

import net.osmand.plus.activities.OsmandApplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dualsportmaps.android.DsmResourceManager;
import com.dualsportmaps.android.R;

public class CacheDsmMapTiles extends Activity {
	private TextView textView;
	private Handler mHandler = new Handler();
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.cache_dsm_map_tiles);
		this.setTitle("Dual Sport Maps Tile Cache Info");
		textView = (TextView) findViewById(R.id.txtCacheTiles);
		Button cancelButton = (Button) findViewById(R.id.CMDcacheTilesCancel);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Builder builder = new AlertDialog.Builder(CacheDsmMapTiles.this);
				builder.setMessage(R.string.cancel_tile_cache);
				builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						DsmResourceManager.getDsmResourceManager((OsmandApplication)getApplication()).cancelTileCaching();
						CacheDsmMapTiles.this.finish();
					}
				});
				builder.setNegativeButton(R.string.default_buttons_no, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builder.show();
			}});
		updateText();
		mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 1000);
	}
	private Runnable mUpdateTimeTask = new Runnable() {
	   public void run() {
		   updateText();
		   mHandler.removeCallbacks(mUpdateTimeTask);
	       mHandler.postDelayed(mUpdateTimeTask, 1000);
	   }
	};
	private void updateText() {
		textView.setText(DsmResourceManager.getDsmResourceManager((OsmandApplication)getApplication()).tilesToCache.size() + " tiles remaining.");
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacks(mUpdateTimeTask);
	}
}
